package cn.kgc.crowdfundingadminspringboot.controller;

import cn.kgc.crowdfundingadminspringboot.common.ResultEntity;
import cn.kgc.crowdfundingadminspringboot.pojo.Advertisement;
import cn.kgc.crowdfundingadminspringboot.service.AdvertisementService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/advertisement")
public class AdvertisementController {
    @Resource
    AdvertisementService advertisementService;

    @RequestMapping("/to/advertisement.html")
    public String toPage(){
        return "advertisement-page";
    }

    /**
     * 显示所有数据
     * @param pageNum
     * @param pageSize
     * @param keyword
     * @return
     */
    @ResponseBody
    @RequestMapping("/query/for/search")
    public ResultEntity<PageInfo<Advertisement>> queryForSearch(

            // 如果页面上没有提供对应的请求参数，那么可以使用defaultValue指定默认值
            @RequestParam(value="pageNum", defaultValue="1") Integer pageNum,
            @RequestParam(value="pageSize", defaultValue="5") Integer pageSize,
            @RequestParam(value="keyword", defaultValue="") String keyword) {

        PageInfo<Advertisement> pageInfo = advertisementService.queryForKeywordSearch(pageNum, pageSize, keyword);
        List<Advertisement> list = pageInfo.getList();
        for (Advertisement advertisement : list) {
            System.out.println(advertisement);
        }
        // 2.封装结果对象返回
        return ResultEntity.successWithoutData(pageInfo);
    }
    /**
     * 添加角色
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping("/save/role")
    public ResultEntity<String> saveRole(String name) {
        System.out.println("name:"+name);
        advertisementService.saveRole(name);
        return ResultEntity.successWithoutData();
    }


    /**
     * 删除
     * @param adminIdList
     * @return
     */
    @ResponseBody
    @RequestMapping("/batch/remove")
    public ResultEntity<String> batchRemove(@RequestBody List<Integer> adminIdList){
        System.out.println("进批量删除的方法了");
        System.out.println("hahaha:"+adminIdList);
        try {
            advertisementService.batchRemove(adminIdList);
            return ResultEntity.successWithoutData();
        }catch(Exception e) {
            return ResultEntity.failed(null,e.getMessage());
        }
    }
    @ResponseBody
    @RequestMapping("/get/list/by/id/list")
    public ResultEntity<List<Advertisement>> selectById(@RequestBody List<Integer> adminIdList){
        System.out.println("jingselectById");
        System.out.println("<========================>"+adminIdList);
        List<Advertisement> advertisements = advertisementService.selectById(adminIdList);
        return ResultEntity.successWithoutData(advertisements);
    }


}
